# 根据接口获取和新增公共参数
from Tools.requests_class import RequestClass
from Lib.common.common_function import *
import pytest
from Lib.common.get_data_by_mysql import MysqlAssert
from Tools.create_params import CreateParams
from Tools.delete_sql import *


def get_department_id():
    # 获取部门id
    sql = 'SELECT org_id FROM info_employee WHERE username="kobekq1"'
    org_id = MysqlAssert().get_data(sql)
    return org_id


def get_grid_id():
    '''
    获取二级网格的id
    :return:
    '''
    # 判断当前网格是否存在，不存在时创建
    sql = 'SELECT id FROM info_grid WHERE NAME = "测试网格_请勿删除"'
    grid_id = MysqlAssert().get_data(sql)
    if not grid_id:
        # 如果查询的网格不存在，先创建
        CreateParams().create_grid()
        # 数据库查询网格id并返回
        return MysqlAssert().get_data(sql)
    # 存在直接返回
    else:
        return grid_id


def get_grid_serial():
    '''
    获取二级网格的serial
    :return:
    '''
    # 判断当前网格是否存在，不存在时创建

    sql = 'SELECT c.serial FROM info_grid c WHERE c.name = "测试网格_请勿删除"'
    grid_serial = MysqlAssert().get_data(sql)
    if not grid_serial:
        # 如果查询的网格不存在，先创建
        CreateParams().create_grid()
        # 数据库查询网格id并返回
        return MysqlAssert().get_data(sql)
    # 存在直接返回
    else:
        return grid_serial


def get_business_event_type():
    '''
    # 判断当前事件是否存在， 不存在先创建再返回
    :return:
    '''
    sql = 'SELECT event_type_code FROM info_event_category WHERE event_type_name = "测试商户小类_请勿删除"'
    event_type_code = MysqlAssert().get_data(sql)
    if not event_type_code:
        # 创建事件小类
        CreateParams().create_business_event_type()
        # 调用接口获取对应的值
        return MysqlAssert().get_data(sql)
    else:
        return event_type_code


def get_site_event_type():
    '''
    # 获取工地事件
    :return:
    '''
    sql = 'SELECT event_type_code FROM info_event_category WHERE event_type_name = "测试新增工地_请勿删除"'
    event_type_code = MysqlAssert().get_data(sql)
    if not event_type_code:
        # 创建工地相关的事件小类
        CreateParams().create_site_event_type()
        # 调用接口获取对应的值
        return MysqlAssert().get_data(sql)
    else:
        return event_type_code


def get_safety_production_event_type():
    '''
    # 获取工地事件
    :return:
    '''
    sql = 'SELECT event_type_code FROM info_event_category WHERE event_type_name = "测试安全生产_请勿删除"'
    event_type_code = MysqlAssert().get_data(sql)
    if not event_type_code:
        # 创建工地相关的事件小类
        CreateParams().create_safety_production_event_type()
        # 调用接口获取对应的值
        return MysqlAssert().get_data(sql)
    else:
        return event_type_code


def create_flow():
    '''
    创建事件流程
    :return:
    '''
    # 判断流程是否存在
    sql = 'SELECT id FROM info_grid_process WHERE NAME = "测试流程_请勿删除"'
    if not MysqlAssert().get_data(sql):
        # 获取用户id
        user_sql = 'SELECT user_id FROM info_employee WHERE username IN ("kobekq1", "kobekq2")'
        user_ids = MysqlAssert().get_datas(user_sql)
        userIds = [user_ids[0][0], user_ids[1][0]]
        data = get_data_by_yaml(get_path() + '/Tools/interface_params/event_flow.yaml')
        # 为userids传值
        for i in range(6):
            data[0]['data']['configs'][i]['userIds'] = userIds
        # 传入网格的值
        data[0]['data']['baseInfo']['gridId'] = get_grid_id()
        # 为部门id传值
        data[0]['data']['baseInfo']['organId'] = get_department_id()
        # 为事件类型传值
        eventTypes = [get_site_event_type(), get_business_event_type(), get_safety_production_event_type()]
        data[0]['data']['baseInfo']['eventTypes'] = eventTypes
        print(data[0]['data'])
        data[0]['headers']['accessToken'] = CreateParams().get_token()
        RequestClass(CreateParams().host, 'post').request(params=data[0])
    else:
        pass


def data_initialize():
    # 数据初始化
    # 创建事件流程
    create_flow()
    # 创建建筑工地
    CreateParams().create_site()
    # 创建商户


def delete_test_data():
    '''
    删除所有的测试数据
    :return:
    '''
    for sql in return_delete_list():
        MysqlAssert().delete_data_by_sql(sql)


if __name__ == '__main__':
    delete_test_data()
